<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 列表控制器
 */
class ListsController extends Controller {
/**
 * 分类列表API
 * @param  typeid 类目ID
 * @param  page   页数
 * @param  brandid 品牌ID
 * return  json
 */
public function TypeItemList(){
		$typeid=I('get.typeid',1,'strip_tags');
		$page=I('get.page',1,'strip_tags');//当前页数
	  	$brandid=I('get.brandid','','strip_tags');
	  	$areatype=I('get.areatype','2','strip_tags');
	  $devicecode=I('get.devicecode','','strip_tags');
	//通过设备寻找运营商ID
	$yid= Operator($devicecode);

	  	//判断当前分类是否是一级分类
	  	$type=M('type');
	  	$pid=$type->where('typeid=%d',$typeid)->getField('pid');
	  	if($pid ===null)
	  	{
	  			$result['status']=0;
				$result['totalpage']='';
				$result['msg']='暂无数据';
				echo json_encode($result,JSON_UNESCAPED_UNICODE);
				return '';
	  	}
	  	$pid=intval($pid);
	  	//一级分类
	  	if($pid===0)
	  	{
		  	//获取二级分类下
		  	$sectype=$type->where('pid=%d',$typeid)->getField('typeid',true);
		  	$where['typeid']=array('in',$sectype);
		  	$where['isdel']=0;
		  	$where['status']=1;///在售状态产品

		  if($areatype==2)
		  {
		  		$where['areatype']=$areatype;
		  		
		  }else{
		  	$where['areatype']=$areatype;
		  	
						if(!empty($yid))
						{
							$where['yid']=$yid;
						}

		  }
		  	$count=M('item')->where($where)->count();
		
		  	$pagearr= getPageArr($page,12,$count);
		  	$totalpage=$pagearr['pagetotal'];
			//获取二级分类的所有产品
			$item=M('item');
			$temp=$item->where($where)->order('itemid desc')->limit($pagearr['start'],$pagearr['pagesize'])->getField('itemid,itemname,price,itemimg,bn');
		
			if (empty($totalpage) || empty($temp)) {
				$result['status']=0;
				$result['totalpage']='';
				$result['msg']='暂无数据';
				echo json_encode($result,JSON_UNESCAPED_UNICODE);
			}else{
				$result['status']=1;
				$result['totalpage']=!empty($totalpage)?$totalpage:0;
				$result['items']=array_values($temp);
				
				echo json_encode($result,JSON_UNESCAPED_UNICODE);

			}
			
  		
  	}else{
  	//当前分类为二级分类
  	//点击左侧品牌时
if($areatype==2){
	
	$where['brandid']=!empty($brandid)?'brandid='.$brandid:'1=1';
  	$where['typeid']='typeid='.$typeid;
  	$where['areatype']='areatype='.$areatype;
  
  	//获取总条数
  	$count=M()->query('select count(*) as count from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].'  AND  status=1');
  
    $count=$count[0]['count'];
    $pagearr= getPageArr($page,12,$count);
    $result['items']=M()->query('select itemid,itemname,price,itemimg,bn from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].' AND  status=1 '.' limit '. $pagearr['start'].','.$pagearr['pagesize'] );
}else{

	$where['brandid']=!empty($brandid)?'brandid='.$brandid:'1=1';
  	$where['typeid']='typeid='.$typeid;
	$where['areatype']='areatype='.$areatype;
    $where['yid']='yid='.$yid;
   
   // var_dump($where);die;
     //获取总条数
 // $count=M()->query('select count(*) as count from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].'  AND  status=1');
     $count=M()->query('select count(*) as count from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].' AND '.$where['areatype'].' AND '.$where['yid'].'  AND  status=1');
  
  
    $count=$count[0]['count'];
    $pagearr= getPageArr($page,12,$count);
  //  $result['items']=M()->query('select itemid,itemname,price,itemimg,bn from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].' AND  status=1 '.' limit '. $pagearr['start'].','.$pagearr['pagesize'] );
     $result['items']=M()->query('select itemid,itemname,price,itemimg,bn from '.C('DB_PREFIX').'item where '.$where['brandid'].' AND '.$where['typeid'].' AND '.$where['areatype'].' AND  status=1 '.' AND '.$where['yid'].' limit '. $pagearr['start'].','.$pagearr['pagesize'] );
}
  	
   

    if(!empty($result['items'])){
    	$result['status']=1;
    	$result['totalpage']=!empty($pagearr['pagetotal'])?$pagearr['pagetotal']:0;
    	echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }else{
    	$result['status']=0;
    	$result['msg']='暂无数据';
    	echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

  	//二级分类end

  	}

	
	
}

/**
 * 多属性多条件筛选输出属性
 * @param  typeid 必须二级分类typeid
 * return json
 */
public function screenSpec(){
	$typeid=I('get.typeid',1,'strip_tags');
	//判断是否是一级分类
	$pid=M('type')->where('typeid=%d',$typeid)->getField('pid');
	$pid=intval($pid);
	if($pid===0){
		//一级分类只筛选品牌属性
		$typeids=M('type')->where('pid=%d',$typeid)->getField('typeid',true);
		//获取每个分类下的品牌
		$tempbrand=array();
		foreach ($typeids as $key => $value)
		{
			$tempbrand[]=M()->query('SELECT DISTINCT (b.brandid),b.brandname,a.typeid FROM '.C('DB_PREFIX').'type_brand a INNER JOIN '.C('DB_PREFIX').'brand b on a.brandid =b.brandid where a.typeid='.$value);
		}
		$temp=array();
		foreach ($tempbrand as $key => $value) 
		{
			foreach ($value as $k => $v) 
			{
				$temp[]=$v;
			}
		}
		if(!empty($temp)){
			$result['status']=1;
			$result['brand']=$temp;
			echo json_encode($result,JSON_UNESCAPED_UNICODE);

		}else{
			$result['status']=0;
			$result['msg']='暂无数据';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
		}


	}else{
			//二级分类下的多属性筛选
			$type_property=M('type_property ');
			$type_propertyvalue=M('type_propertyvalue');
			$temp=array();
			$propertyids=$type_property->where('typeid=%d',$typeid)->getField('propertyid,propertyname');
			$i=0;
			foreach ($propertyids as $key => $value)
			 {
				
				$temp[$i]['propertyid']=$key;
				$temp[$i]['propertyname']=$value;
				$temp[$i]['propertyvalue']=M()->query('select valueid,propertyvalue from '.C('DB_PREFIX').'type_propertyvalue where propertyid='.$key);
				$i+=1;
			}
			$result=array();
			if(!empty($temp)){
				$result['status']=2;
				$result['property']=$temp;
			}else{
				$result['status']=0;
				$result['msg']='暂无数据';
			}

			echo json_encode($result,JSON_UNESCAPED_UNICODE);

	}
}
/*
 ×多条件筛选输出结果
  *@param  json (属性名；属性值；类目id ; 品牌id;)
 */
public function screenItem()
{   $result=array();
	
	//$json='{"typeid": "1","status": "2","brand": [{ "brandid": "70"},{"brandid": "71"},{"brandid": "74"}]}';
	///$json='{ "property":{"141": [676, 677], "142": [678], "143": [681], "148": [ ]} ,  "status": "1",  "typeid": "72"}';
	$json=!empty($_GET['shuxing'])?$_GET['shuxing']:'';
	$hprice=!empty($_GET['hprice'])?intval($_GET['hprice']):'';
	$lprice=!empty($_GET['lprice'])?intval($_GET['lprice']):'';
	$page=I('get.page',1,'strip_tags');//请求页数
	$areatype=I('get.areatype',2,'strip_tags');

	$devicecode=I('get.devicecode','','strip_tags');
	//通过设备寻找运营商ID
	$yid= Operator($devicecode);



	$json=json_decode($json,true);  


	if (!empty($json)) 
	{	
			$json_status=$json['status'];

			$typeid= intval($json['typeid']);
			$item=M('item');
			switch ($json_status) {
				case "2":
				    $brandid=$json['brand'];
					if (!empty($brandid)) 
					{	
						
						$brandids=array();
						foreach($brandid as $key=>$value){
								$brandids[]= intval($value['brandid']);
						}
						$where['brandid']=array('in',$brandids);
						
					}
					if(!empty($hprice) && !empty($lprice))
						{
							$where['price']=array('between',array($lprice,$hprice));
						}


					//获取一级类目下的二级类目
					if (!empty($typeid))
					 {
						$typeids=array();

						$typeids=M('type')->where('pid=%d',$typeid)->getField('typeid',true);

						$where['typeid']=array('in',$typeids);
					}
					//商品区域
					if($areatype=='2')
					{
						$where['areatype']=$areatype;

					}else
					{
						$where['areatype']=$areatype;
						if(!empty($yid))
						{
							$where['yid']=$yid;
						}

					}
					$where['status']=1;
					$where['logic']="AND";
					$count=$item->where($where)->count();
					$pagearr= getPageArr($page,C('PAGESIZE'),$count);
					$totalpage=$pagearr['pagetotal'];

					$items=$item->where($where)
								->order('itemid desc')
								->limit($pagearr['start'],$pagearr['pagesize'])
					            ->getField('itemid,itemname,itemimg,price,bn');
					            

					break;
				
				case '1':
					//筛选数据值包含品牌值和typeid、属性名、属性值
					
					$property=$json['property'];

					if (!empty($property))
					{   
						$propertyid=array();
						$propertyvalueid=array();
						foreach($property as $key=>$value){
							if (!empty($value)) 
							{
								$propertyid[]=$key;
								foreach($value as $k=>$v)
								{	
									$propertyvalueid[]=$v;
								}
							}
						}
						if(!empty($hprice) && !empty($lprice) )
						{
							$where['b.price']=array('between',array($lprice,$hprice));
						}
						//商品区域
						if($areatype==2){
							$where['b.areatype']=$areatype;

						}else{
							

							$where['b.areatype']=$areatype;
							if(!empty($yid))
							{
								$where['b.yid']=$yid;
							}
							
						}

						$where['b.typeid']=!empty($typeid)?$typeid:'';
                        $where['b.status']  =1;//在售状态
						$where[' a.`propertyid`']=!empty($propertyid)?array('in',$propertyid):'';
						$where['a.propertyvalueid']=!empty($propertyvalueid)?array('in',$propertyvalueid):'';
						//$where['logic']="AND";
						$count=M('item_property a')->join('inner join '.C('DB_PREFIX').'item  b ON a.`itemid`=b.`itemid`')
						                          ->where($where)
						                          ->count();

						$pagearr= getPageArr($page,12,$count);

						$totalpage=$pagearr['pagetotal'];
						
						$items=M('item_property a')->distinct ( true )->join('inner join '.C('DB_PREFIX').'item  b ON a.`itemid`=b.`itemid`')
						                          ->where($where)
						                          ->order('b.itemid desc')
						                          ->limit($pagearr['start'],$pagearr['pagesize'])
						                          ->getField('b.`itemid`, b.`itemname`,b.`price`,b.itemimg,b.bn');
						
						$items=!empty($items)?array_values($items):array();
					}else
					{   $where['status']=1;////在售状态
						$where['price']=array('between',array($lprice,$hprice));
						$count=M('item')->where($where)->count();

						$pagearr= getPageArr($page,C('PAGESIZE'),$count);

						$totalpage=$pagearr['pagetotal'];

						$items=M('item')->where($where)
				                        ->order('itemid desc')
				                        ->limit($pagearr['start'],$pagearr['pagesize'])
						                ->getField('itemid,itemname,price,itemimg,bn');
						
					}
					
					break;
			}

	}else{
		//只筛选价格
		//商品区域
		if($areatype==2){
			$where['areatype']=$areatype;

		}else{
				$where['areatype']=$areatype;
				if(!empty($yid))
				{
					$where['yid']=$yid;
				}
		}
		$where['status']=1;///在售状态
		$where['price']=array('between',array($lprice,$hprice));
		$count=M('item')->where($where)->count();
		$pagearr= getPageArr($page,C('PAGESIZE'),$count);

		$totalpage=$pagearr['pagetotal'];

		$items=M('item')->where($where)
                        ->order('itemid desc')
                        ->limit($pagearr['start'],$pagearr['pagesize'])
		                ->getField('itemid,itemname,price,itemimg,bn');
		
	}
		
		//输出结果
		$result['status']=1;
		$result['totalpage']=!empty($totalpage)?$totalpage:0;
		$result['items']=!empty($items)?array_values($items):array();
		echo json_encode($result,JSON_UNESCAPED_UNICODE);

}


}